package linkedlist;

import org.junit.jupiter.api.Test;

public class SingleLinkedList {

    @Test
    public void reversalSingleNode() {

        SingleNode singleNode1 = new SingleNode(1);
        SingleNode singleNode2 = new SingleNode(2);
        SingleNode singleNode3 = new SingleNode(3);
        SingleNode singleNode4 = new SingleNode(4);
        singleNode1.next = singleNode2;
        singleNode2.next = singleNode3;
        singleNode3.next = singleNode4;
        System.out.println("=================反转前链表==================");
        System.out.println(singleNode1);
        SingleNode singleNode = doReversalSingleNode(singleNode1);
        System.out.println("=================反转后链表==================");
        System.out.println(singleNode);
    }

    private SingleNode doReversalSingleNode(SingleNode head) {
        SingleNode pre = null;
        SingleNode next = null;
        while (head != null) {
            next = head.next;
            head.next = pre;
            pre = head;
            head = next;
        }
        return pre;
    }

    static class SingleNode {
        private final Integer data;
        private SingleNode next;

        public SingleNode(Integer data) {
            this.data = data;
        }

        @Override
        public String toString() {
            return "SingleNode{" +
                    "next=" + next +
                    ", data=" + data +
                    '}';
        }
    }
}
